/**
 * FileName: Exercise1932.c
 * ----------------------------------------------------------------------------------------------------
 *  19.32 Draw the DFS forest that results from a standard adjacency-matrix DFS of the digraph
    ```txt
    3-7 1-4 7-8 0-5 5-2 3-8 2-9 0-6 4-9 2-6 6-4.
    ```
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

//边相关
typedef struct {
    int v;
    int w;
}
Edge;

//图相关
typedef struct graph *Graph;
struct graph {
    int V;
    int E;
    int** adj;
};

#define dfsR2 search

static int cnt;
static int* pre;
//Program 19.2:新增post数组和计数器cntP
static int cntP;
static int* post;

int depth = 0; // 全局变量表示递归深度，用来控制缩进量

//辅助函数声明
void HELPinit(int);
void HELPcleanUp();
int** MATRIXinit(int, int, int);
Edge EDGE(int, int);
void GRAPHshow(Graph);
void dfsR(Graph, Edge);
void printIndent();
void dfsR2(Graph, Edge);
void show(char*, Edge);

//图操作声明
Graph GRAPHinit(int);
void GRAPHinsertE(Graph, Edge);
void GRAPHremoveE(Graph, Edge);
int GRAPHedges(Edge [], Graph);
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);
void GRAPHsearch(Graph);

//辅助函数实现
void HELPinit(int maxV) {
    //全局变量初始化
    cnt = 0;
    int v;
    pre = malloc(maxV * sizeof(int));
    for (v = 0; v < maxV; v++) {
        pre[v] = -1;
    }
    cntP = 0;
    post = malloc(maxV * sizeof(int));
    for (v = 0; v < maxV; v++) {
        post[v] = -1;
    }

    depth = 0;
}

void HELPcleanUp() {
    free(pre);
    free(post);
}
/**
 * Program 17.4 Adjacency-matrix allocation and initialization
 * -------------------------------------------------------------------------------------------------------------
 * This program uses the standard C array-of-arrays representation for the two-dimensional adjacency matrix (see Section 3.7).
 * It allocates `r` rows with `c` integers each, then initializes all entries to the value `val`.
 *
 * The call `MATRIXinit(V, V, 0)` in Program 17.3 takes time proportional to $V^2$ to create a matrix that
 * represents a V-vertex graph with no edges.
 *
 * For small $V$, the cost of $V$ calls to `malloc` might predominate.
 */
int** MATRIXinit(int r, int c, int val) {
    int i;
    int j;
    int** t = malloc(r * sizeof(int*));
    for (i = 0; i < r; i++) {
        t[i] = malloc(c * sizeof(int));
    }
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            t[i][j] = val;
        }
    }
    return t;
}

Edge EDGE(int v, int w) {
    Edge edge;
    edge.v = v;
    edge.w = w;
    return edge;
}

void GRAPHshow(Graph G) {
    int i;
    int j;
    printf("%d vertices, %d edges\n", G->V, G->E);

    //邻接列表
    // for (i = 0; i < G->V; i++) {
    //     printf("%2d:", i);
    //     for (j = 0; j < G->V; j++) {
    //         if (G->adj[i][j] == 1) {
    //             printf(" %2d", j);
    //         }
    //     }
    //     printf("\n");
    // }
    //邻接矩阵
    for (i = 0; i < G->V; i++) {
        printf("%2d:", i);
        for (j = 0; j < G->V; j++) {
            printf(" %2d", G->adj[i][j]);
        }
        printf("\n");
    }
}

void show(char* key, Edge e) {
    printIndent();
    printf("%d-%d %s\n", e.v, e.w, key);
}

/**
 * Program 19.2 DFS of a digraph
 * ---------------------------------------------------------------------------------------------------------------------
 * This DFS function for digraphs represented with adjacency lists is instrumented to show the role that
 * each edge in the graph plays in the DFS.
 *
 * It assumes that Program 18.3 is augmented to declare and initialize the array `post` and
 * the counter `cntP` in the same way as `pre` and `cnt`, respectively.
 *
 * See Figure 19.10 for sample output and a discussion about implementing show.
 *
 */
void dfsR(Graph G, Edge e) {
    int t;
    int w = e.w;
    Edge x;
    pre[w] = cnt++;

    for (t = 0; t < G->V; t++) {
        if (G->adj[w][t] != 0) {
            if (pre[t] == -1) {
                // tree link
                dfsR(G, EDGE(w, t));
            }else {
                x = EDGE(w, t);
                if (post[t] == -1) {
                    // back link
                }else if (pre[t] > pre[w]) {
                    // down link
                }else {
                    // cross link
                }
            }
        }
    }

    post[w] = cntP++;

}

void printIndent() {
    int i;
    for (i = 0; i < depth; i++) {
        printf("  "); // 每层缩进2个空格
    }
}

/**
 * Program 19.2 DFS of a digraph
 * ---------------------------------------------------------------------------------------------------------------------
 * This DFS function for digraphs represented with adjacency lists is instrumented to show the role that
 * each edge in the graph plays in the DFS.
 *
 * It assumes that Program 18.3 is augmented to declare and initialize the array `post` and
 * the counter `cntP` in the same way as `pre` and `cnt`, respectively.
 *
 * See Figure 19.10 for sample output and a discussion about implementing show.
 *
 */
void dfsR2(Graph G, Edge e) {
    int t;
    int w = e.w;
    Edge x;
    show("tree", e);
    depth++;
    pre[w] = cnt++;

    for (t = 0; t < G->V; t++) {
        if (G->adj[w][t] != 0) {
            if (pre[t] == -1) {
                // tree link
                dfsR2(G, EDGE(w, t));
            }else {
                x = EDGE(w, t);
                if (post[t] == -1) {
                    // back link
                    show("back", x);
                }else if (pre[t] > pre[w]) {
                    // down link
                    show("down", x);
                }else {
                    // cross link
                    show("cross", x);
                }
            }
        }
    }

    post[w] = cntP++;
    depth--;
}

//图操作函数实现
Graph GRAPHinit(int V) {
    Graph G = malloc(sizeof(*G));
    G->V = V;
    G->E = 0;
    G->adj = MATRIXinit(V, V, 0);
    return G;
}

void GRAPHinsertE(Graph G, Edge e) {
    int v = e.v;
    int w = e.w;
    G->adj[v][w] = 1;
    G->E++;
}

void GRAPHremoveE(Graph G, Edge e) {
    int v = e.v;
    int w = e.w;
    G->adj[v][w] = 0;
    G->E--;
}
int GRAPHedges(Edge a[], Graph G) {
    int v;
    int w;
    int E = 0;
    for (v = 0; v < G->V; v++) {
        for (w = v+1; w < G->V; w++) {
            if (G->adj[v][w] == 1) {
                a[E++] = EDGE(v, w);
            }
        }
    }
    return E;
}

Graph GRAPHcopy(Graph G) {
    Graph copy = GRAPHinit(G->V);
    int i;
    int j;
    for (i = 0; i < G->V; i++) {
        for (j = 0; j < G->V; j++) {
            if (G->adj[i][j] != 0) {
                GRAPHinsertE(copy, EDGE(i, j));
            }
        }
    }
    return copy;

}

void GRAPHdestroy(Graph G) {
    int i;
    for (i = 0; i < G->V; i++) {
        free(G->adj[i]);
    }
    free(G->adj);
    free(G);
}

void GRAPHsearch(Graph G) {
    int v;
    HELPinit(G->V);
    for (v = 0; v < G->V; v++) {
        if (pre[v] == -1) {
            search(G, EDGE(v,v));
        }
    }
}

void GRAPHsearch2(Graph G, int s) {
    int v = s;
    HELPinit(G->V);
    printf("DFS forest: %d as start vertex\n", s);
    int count = 0;
    while (count < G->V) {
        if (pre[v] == -1) {
            search(G, EDGE(v,v));
        }
        v++;
        v=v%G->V;
        count++;

    }
    printf("------------------------------------------------------------\n");
}

//测试函数声明
void test_dfs_forest();

int main(int argc, char *argv[]) {
    test_dfs_forest();
    return 0;
}

//测试函数实现
void test_dfs_forest() {
    int V = 10;
    Graph G = GRAPHinit(V);

    GRAPHinsertE(G, EDGE(3, 7));
    GRAPHinsertE(G, EDGE(1, 4));
    GRAPHinsertE(G, EDGE(7, 8));
    GRAPHinsertE(G, EDGE(0, 5));
    GRAPHinsertE(G, EDGE(5, 2));
    GRAPHinsertE(G, EDGE(3, 8));
    GRAPHinsertE(G, EDGE(2, 9));
    GRAPHinsertE(G, EDGE(0, 6));
    GRAPHinsertE(G, EDGE(4, 9));
    GRAPHinsertE(G, EDGE(2, 6));
    GRAPHinsertE(G, EDGE(6, 4));

    // GRAPHshow(G);
    int s;
    for (s = 0; s < G->V; s++) {
        GRAPHsearch2(G, s);
    }
    HELPcleanUp();
}